<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="父级ID" prop="parentId">
        <el-input v-model="dataForm.parentId" placeholder="父级ID"></el-input>
      </el-form-item>
      <el-form-item label="区域全名称" prop="fullName">
        <el-input v-model="dataForm.fullName" placeholder="区域全名称"></el-input>
      </el-form-item>
      <el-form-item label="区域名称" prop="regionName">
        <el-input v-model="dataForm.regionName" placeholder="区域名称"></el-input>
      </el-form-item>
      <el-form-item label="区域级别 0,中国；1，省分；2，市；3，区、县" prop="regionLevel">
        <el-input v-model="dataForm.regionLevel" placeholder="区域级别 0,中国；1，省分；2，市；3，区、县"></el-input>
      </el-form-item>
      <el-form-item label="删除标志,1为未删除,-1为删除" prop="delFlag">
        <el-input v-model="dataForm.delFlag" placeholder="删除标志,1为未删除,-1为删除"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="memo">
        <el-input v-model="dataForm.memo" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="序号" prop="sortNum">
        <el-input v-model="dataForm.sortNum" placeholder="序号"></el-input>
      </el-form-item>
      <el-form-item label="区域代码" prop="regionCode">
        <el-input v-model="dataForm.regionCode" placeholder="区域代码"></el-input>
      </el-form-item>
      <el-form-item label="简称" prop="shortName">
        <el-input v-model="dataForm.shortName" placeholder="简称"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="创建人" prop="createUser">
        <el-input v-model="dataForm.createUser" placeholder="创建人"></el-input>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
      </el-form-item>
      <el-form-item label="修改人" prop="updateUser">
        <el-input v-model="dataForm.updateUser" placeholder="修改人"></el-input>
      </el-form-item>
      <el-form-item label="租户id" prop="tenantId">
        <el-input v-model="dataForm.tenantId" placeholder="租户id"></el-input>
      </el-form-item>
      <el-form-item label="城市代码" prop="cityCode">
        <el-input v-model="dataForm.cityCode" placeholder="城市代码"></el-input>
      </el-form-item>
      <el-form-item label="邮编" prop="zipCode">
        <el-input v-model="dataForm.zipCode" placeholder="邮编"></el-input>
      </el-form-item>
      <el-form-item label="经度" prop="lng">
        <el-input v-model="dataForm.lng" placeholder="经度"></el-input>
      </el-form-item>
      <el-form-item label="纬度" prop="lat">
        <el-input v-model="dataForm.lat" placeholder="纬度"></el-input>
      </el-form-item>
      <el-form-item label="拼音" prop="pinyin">
        <el-input v-model="dataForm.pinyin" placeholder="拼音"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { getObj, addObj, putObj } from '@/api/pvm/region'

  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          parentId: '',
          fullName: '',
          regionName: '',
          regionLevel: '',
          delFlag: '',
          memo: '',
          sortNum: '',
          regionCode: '',
          shortName: '',
          createTime: '',
          createUser: '',
          updateTime: '',
          updateUser: '',
          tenantId: '',
          cityCode: '',
          zipCode: '',
          lng: '',
          lat: '',
          pinyin: ''
        },
        dataRule: {
          parentId: [{
            required: true,
            message: '父级ID不能为空',
            trigger: 'blur'
          }],
          fullName: [{
            required: true,
            message: '区域全名称不能为空',
            trigger: 'blur'
          }],
          regionName: [{
            required: true,
            message: '区域名称不能为空',
            trigger: 'blur'
          }],
          regionLevel: [{
            required: true,
            message: '区域级别 0,中国；1，省分；2，市；3，区、县不能为空',
            trigger: 'blur'
          }],
          delFlag: [{
            required: true,
            message: '删除标志,1为未删除,-1为删除不能为空',
            trigger: 'blur'
          }],
          memo: [{
            required: true,
            message: '备注不能为空',
            trigger: 'blur'
          }],
          sortNum: [{
            required: true,
            message: '序号不能为空',
            trigger: 'blur'
          }],
          regionCode: [{
            required: true,
            message: '区域代码不能为空',
            trigger: 'blur'
          }],
          shortName: [{
            required: true,
            message: '简称不能为空',
            trigger: 'blur'
          }],
          createTime: [{
            required: true,
            message: '创建时间不能为空',
            trigger: 'blur'
          }],
          createUser: [{
            required: true,
            message: '创建人不能为空',
            trigger: 'blur'
          }],
          updateTime: [{
            required: true,
            message: '修改时间不能为空',
            trigger: 'blur'
          }],
          updateUser: [{
            required: true,
            message: '修改人不能为空',
            trigger: 'blur'
          }],
          tenantId: [{
            required: true,
            message: '租户id不能为空',
            trigger: 'blur'
          }],
          cityCode: [{
            required: true,
            message: '城市代码不能为空',
            trigger: 'blur'
          }],
          zipCode: [{
            required: true,
            message: '邮编不能为空',
            trigger: 'blur'
          }],
          lng: [{
            required: true,
            message: '经度不能为空',
            trigger: 'blur'
          }],
          lat: [{
            required: true,
            message: '纬度不能为空',
            trigger: 'blur'
          }],
          pinyin: [{
            required: true,
            message: '拼音不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putObj(this.dataForm).then(data => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            } else {
              addObj(this.dataForm).then(data => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            }
          }
        })
      }
    }
  }
</script>
